package com.zc.study.thread.多线程第四章节.thread4_1_7;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Service {
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    private boolean flag = false;
    public void a(){
        try {
        lock.lock();
        while (flag){
             condition.await();
        }
        System.out.println("print a");
        flag = true;
        condition.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    public void b(){
        try {
        lock.lock();
        while (!flag){
             condition.await();
        }
        System.out.println("print b");
        flag = false;
        condition.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
